//---------------------------------
// Questions and Answers
//

var page = null;

function load(){
	page = new BridgeKeeper();
	page.load();
}

var BridgeKeeper = new Class ({
	// page opener
	load: function() {
		this.setupFormForAjax("BoD");
		$('question').set('text','What is your name?');
	},
	
	// set up form for MooTool AJAX 
	setupFormForAjax: function(form_id) {
		frm = $$("#"+form_id+" form");
		
		frm.addEvent("submit",function(ev) {
			ev.stop();
			
			this.set('send', {
	
	            onSuccess: function(txt,xml) {
	            	console.log(txt);
	            	// parse results text
	            	rs = JSON.decode(txt)
	            	
	            	switch(rs.qno) {
	            		case -1:            			
	            			page.die(rs.ans);
	            			break;
	            		case 0:
	            			page.cross(rs.ans);
	            			break;
	            		default:
	                    	$('question').set('text',rs.ans);
	            		    $('answer').set("value",'')
	            			$$('input[name="qno"]').set('value',rs.qno);
	            	}
	            }
	
	        }).send();
		});
	},
	
	// cast into the Gorge of Eternal Peril
	die:  function(msg) {
		$$('h1').set('text','You have been cast into the Gorge of Eternal Peril!');
		this.finish();
	},
	
	// Good to go.
	cross:  function(msg) {
		$$('h1').set('text',"Alright then, off you go...");
		this.finish();
	},
	
	finish: function() {
		$('question').set('class','isHidden');
		$('BoD').set('class','isHidden');
		$('reset').set('class','isVisible');
	}
});